#!/usr/bin/env python
#
#       Sounds.py
#       
#       Copyright 2009 Robin Hayes <bonzairob@hotmail.co.uk>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import pygame

import Options

from Client import Client
client = Client()

class Sounds():
    def __init__(self):
        #Let's get the player-set stuff.
        self.musvol = Options.VOLUME_MUSIC
        self.sfxvol = Options.VOLUME_SFX
        self.mute = not Options.SOUND
        
        #Make the variables for the sounds
        self.s_pawncann = None
        self.s_newturn = None
        self.m_cogs = None
        self.m_bat1 = None
        self.s_hit1 = None
        self.s_crit1 = None

        if self.mute:
            print "Muted."
            #Don't bother loading sounds if the player doesn't want them.
        else:
            
            #TODO: Method for the laoding of sounds before a battle, instead of loading it all at once.
            
            pygame.mixer.init()
            #AAAH WHY HAS IT FROZEN
            print "Loading sounds."
            
            #We need channels to control stuff.
            #two channels for sfx, firing and hit, for now.
            self.c_sfx = pygame.mixer.Channel(0)
            self.c_mus = pygame.mixer.Channel(1)
            self.c_sfx2 = pygame.mixer.Channel(2)
            self.c_sfx.set_volume(self.sfxvol)
            self.c_mus.set_volume(self.musvol)
            self.c_sfx2.set_volume(self.sfxvol)
            
            #Sounds to go into channels. s for sfx, m for music, but are interchangable.
            soundpaths = []
            
            soundpaths.append(os.path.join("Data", "Sounds", "steamcannons.ogg"));
            soundpaths.append(os.path.join("Data", "Sounds", "nextturn.ogg"));
            soundpaths.append(os.path.join("Data", "Sounds", "Cogs.ogg"));
            soundpaths.append(os.path.join("Data", "Sounds", "Battle1.ogg"));
            soundpaths.append(os.path.join("Data", "Sounds", "hit.ogg"));
            soundpaths.append(os.path.join("Data", "Sounds", "crit.ogg"));
            
            #Clunky way to check if any files are missing.
            failcount = 0
            for path in soundpaths:
                if not os.path.isfile(path):
                     print "Sound ["+path+"] not found. Muting all audio."
                     self.mute = True
                     failcount += 1
                    

            if failcount == 0:
                self.s_pawncann = pygame.mixer.Sound(os.path.join("Data", "Sounds", "steamcannons.ogg"))
                self.s_newturn = pygame.mixer.Sound(os.path.join("Data", "Sounds", "nextturn.ogg"))
                self.m_cogs = pygame.mixer.Sound(os.path.join("Data", "Sounds", "Cogs.ogg"))
                self.m_bat1 = pygame.mixer.Sound(os.path.join("Data", "Sounds", "Battle1.ogg"))
                self.s_hit1 = pygame.mixer.Sound(os.path.join("Data", "Sounds", "hit.ogg"))
                self.s_crit1 = pygame.mixer.Sound(os.path.join("Data", "Sounds", "crit.ogg"))
                

            
    def sfx(self, effect):
        if not self.mute:
            if effect.get_length > 0:
                self.c_sfx.play(effect)
    def sfx2(self, effect):
        if not self.mute:
            if effect.get_length > 0:
                self.c_sfx2.play(effect)

    def music(self, tune, loop):
        if not self.mute:
            if tune.get_length > 0:
                if loop == "":
                    self.c_mus.play(tune)
                else:
                    self.c_mus.play(tune, loop)
                
    def musicfade(self):
        if not self.mute:
            self.c_mus.fadeout(100)
        
    def changeVolume(self, channel, value):
        #For later, with GUI options. SLIDERZ YEAH
        if value <= 1 and value >= 0:
            if channel == "music":
                self.musvol = value
            elif channel == "sfx":
                self.sfxvol = value
            if self.sfxvol == 0 and self.musvol == 0:
                self.mute = True
